/*
* pps_bom
*/
package com.pitop.assembly.service;

import com.pitop.assembly.api.AssemblyFeignApi;
import com.pitop.assembly.api.AssemblyMaterialFeignApi;
import com.pitop.assembly.entity.Assembly;
import com.pitop.assembly.entity.AssemblyMaterial;
import com.pitop.assembly.entity.AssemblyState;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.AssemblyMaterialException;
import com.pitop.core.exceptions.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 构成组件的物料
 *
 * @author berton
 */
@Service("assemblyMaterialSVImpl")
@Slf4j
public class AssemblyMaterialSVImpl {

    @Autowired
    private AssemblyMaterialFeignApi assemblyMaterialFeignApi;

    @Autowired
    private AssemblyFeignApi assemblyFeignApi;

    /**
     * 加载一个对象AssemblyMaterial
     *
     * @param id * @param assemblyCode 组件编码* @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    public AssemblyMaterial load(Long id, String assemblyCode, String materialCode) {
        if (id == null && assemblyCode == null && materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.load(id, assemblyCode, materialCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过id,(将查询关联数据)
     *
     * @param id
     * @return AssemblyMaterial
     */
    public AssemblyMaterial loadById(Long id) {
        if (id == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.loadById(id);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过assemblyCode,(将查询关联数据)
     *
     * @param assemblyCode 组件编码
     * @return AssemblyMaterial
     */
    public AssemblyMaterial loadByAssemblyCode(String assemblyCode) {
        if (assemblyCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.loadByAssemblyCode(assemblyCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过materialCode,(将查询关联数据)
     *
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    public AssemblyMaterial loadByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.loadByMaterialCode(materialCode);
    }

    /**
     * 加载一个对象AssemblyMaterial,(将查询关联数据)
     *
     * @param id * @param assemblyCode 组件编码* @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    public AssemblyMaterial get(Long id, String assemblyCode, String materialCode) {
        if (id == null && assemblyCode == null && materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.get(id, assemblyCode, materialCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过id,(将查询关联数据)
     *
     * @param id
     * @return AssemblyMaterial
     */
    public AssemblyMaterial getById(Long id) {
        if (id == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.getById(id);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过assemblyCode,(将查询关联数据)
     *
     * @param assemblyCode 组件编码
     * @return AssemblyMaterial
     */
    public AssemblyMaterial getByAssemblyCode(String assemblyCode) {
        if (assemblyCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.getByAssemblyCode(assemblyCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过materialCode,(将查询关联数据)
     *
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    public AssemblyMaterial getByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialFeignApi.getByMaterialCode(materialCode);
    }

    /**
     * 删除对象AssemblyMaterial
     *
     * @param id * @param assemblyCode 组件编码* @param materialCode 物料编码
     */

    public void delete(Long id, String assemblyCode, String materialCode) {
        if (id == null ) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
         Assembly assembly = assemblyFeignApi.loadByCode(assemblyCode);
        if(assembly == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        if(AssemblyState.Stable == AssemblyState.getEnum(assembly.getState())) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Assembly_State_Is_Stable);
        }
        if(AssemblyState.Stop == AssemblyState.getEnum(assembly.getState())) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Assembly_State_Is_Stop);
        }
        assemblyMaterialFeignApi.delete(id, assemblyCode, materialCode);
    }

    /**
     * 查询AssemblyMaterial分页
     *
     * @param assemblyMaterial 对象
     * @param curPage          当前页
     * @param pageSize         分页条数
     * @return List<AssemblyMaterial>
     */
    public List<AssemblyMaterial> list(AssemblyMaterial assemblyMaterial, int curPage, int pageSize) {
        return assemblyMaterialFeignApi.list(assemblyMaterial, curPage, pageSize);
    }

    /**
     * 查询AssemblyMaterial分页
     *
     * @param id       * @param assemblyCode  组件编码* @param materialCode  物料编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<AssemblyMaterial>
     */
    public List<AssemblyMaterial> list(Long id, String assemblyCode, String materialCode, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        AssemblyMaterial assemblyMaterial = new AssemblyMaterial();
        assemblyMaterial.setId(id);
        assemblyMaterial.setAssemblyCode(assemblyCode);
        assemblyMaterial.setMaterialCode(materialCode);

        return assemblyMaterialFeignApi.list(assemblyMaterial, curPage, pageSize);
    }

    public int count(Long id, String assemblyCode, String materialCode) {
        return assemblyMaterialFeignApi.count(id, assemblyCode, materialCode);
    }

    public int count(AssemblyMaterial assemblyMaterial) {
        return assemblyMaterialFeignApi.count(assemblyMaterial);
    }

    /**
     * 保存
     *
     * @param assemblyMaterial 实体
     * @throws BaseException
     */
    public void save(AssemblyMaterial assemblyMaterial) {
        if (assemblyMaterial == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyMaterialFeignApi.build(assemblyMaterial);
    }

    /**
     * 更新
     *
     * @param assemblyMaterial 实体
     * @throws BaseException
     */
    public void modify(AssemblyMaterial assemblyMaterial) {
        if (assemblyMaterial == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyMaterialFeignApi.modify(assemblyMaterial);
    }
}
